//
//  ShicaiViewController.m
//  KCZ_B段项目
//
//  Created by dllo on 15/11/3.
//  Copyright © 2015年 Sky. All rights reserved.
//

#import "ShicaiViewController.h"
#import "HTTPMethod.h"
#import "changeString.h"
#import "FitHeader.h"
#import "HeaderCollectionReusableView.h"//自定义头视图
#import "KyShicaiCollectionCell.h"
#import "KyShicaiCollectionCell2.h"
#import "KyShicaiModel.h"//食材类型model
#import <UIImageView+WebCache.h>
#import "FontSizeSingleTon.h"//字号单例
#define FontSize [[FontSizeSingleTon shareFontSize]array]
#import "KyShicaiDetailVController.h"//食材详情页
//#import "NimaCollectionViewCell.h"
#import "KDetailCollectionCell1.h"
#import <MBProgressHUD.h>
#import "KSearchViewController.h"
@interface ShicaiViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property(nonatomic, strong)UICollectionView *collectionview;
@property(nonatomic, strong)NSMutableArray *dataSourceArray;
@property(nonatomic, strong)UICollectionViewFlowLayout *flowLayout;
@property(nonatomic, strong)NSMutableArray *sectionDataArray;
@property(nonatomic, strong)MBProgressHUD *progressView;
@end

@implementation ShicaiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.29 green:0.75 blue:0.19 alpha:1];
    //navigationBar上字的颜色为白色
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],UITextAttributeTextColor, nil]];
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];//navigationcontroller上的颜色为白色
    self.title = @"食材";
    _progressView = [[MBProgressHUD alloc]initWithView:self.view];
    _progressView.labelText = @"加载中...";
    [_progressView setMode:MBProgressHUDModeIndeterminate];
    [_progressView show:YES];
    [self.view addSubview:_progressView];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"sousuo@2x"] style:UIBarButtonItemStylePlain target:self action:@selector(pushSearchViewController)];
    // Do any additional setup after loading the view.
}
-(void)pushSearchViewController
{
    KSearchViewController *ksvc = [[KSearchViewController alloc]init];
    [self.navigationController pushViewController:ksvc animated:YES];
}
-(void)loadView
{
    [super loadView];
 

    [self getData];
    _flowLayout = [[UICollectionViewFlowLayout alloc]init];
//    _flowLayout.itemSize = CGSizeMake(60 * wt, 40 * ht);
    //最小行间距
    _flowLayout.minimumLineSpacing = 10 * wt;
    //最小列间距
    _flowLayout.minimumInteritemSpacing = 10 * ht;
    //设置上左下右的距离
    _flowLayout.sectionInset = UIEdgeInsetsMake(10, 10 , 10, 10);
    
    _collectionview = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) collectionViewLayout:_flowLayout];
    _collectionview.delegate =self;
    _collectionview.dataSource = self;
    _collectionview.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_collectionview];
    
//    [_collectionview registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"identifier"];//重用
    
    [_collectionview registerClass:[HeaderCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"identifier2"];//注册自定义头视图
    [_collectionview registerClass:[KyShicaiCollectionCell  class] forCellWithReuseIdentifier:@"identifier3"];
    [_collectionview registerClass:[KyShicaiCollectionCell2 class] forCellWithReuseIdentifier:@"identifier4"];
    
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    KyShicaiModel *model = [[KyShicaiModel alloc]init];
    model = _sectionDataArray[indexPath.section];
    KyShicaiDetailVController *shicaiVC = [[KyShicaiDetailVController alloc]init];
    shicaiVC.vegetableID = [[model.data objectAtIndex:indexPath.row - 1] objectForKey:@"id"];
    shicaiVC.navTitle = [[model.data objectAtIndex:indexPath.row -1] objectForKey:@"text"];
    [self.navigationController pushViewController:shicaiVC animated:YES];
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        KyShicaiCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"identifier3" forIndexPath:indexPath];
        cell.userInteractionEnabled = NO;
        cell.shicaiTypeModel = _sectionDataArray[indexPath.section];
                return cell;
    }
    else
    {
    KyShicaiCollectionCell2 *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"identifier4" forIndexPath:indexPath];
    KyShicaiModel *model = [[KyShicaiModel alloc]init];
    model = _sectionDataArray[indexPath.section];
    cell.label.text = [[model.data objectAtIndex:indexPath.row -1] objectForKey:@"text"];//第一个图片为第一个cell所以赋值时候减一
    return cell;
    }
}

-(void)getData
{
    _dataSourceArray = [NSMutableArray array];
    _sectionDataArray = [NSMutableArray array];
    NSString *POSTString = @"http://api.izhangchu.com/";
    NSString *body = @"methodName=MaterialSubtype&version=4.02";
    [HTTPMethod getDataByString:POSTString BodyDic:[changeString changeStringToDictionary:body] WithDataBlock:^(id data) {
        NSDictionary *allDataDic = [data objectForKey:@"data"];
         NSArray *typeArray = [allDataDic objectForKey:@"data"];
        NSDictionary *dic = [NSDictionary dictionary];
        for (dic in typeArray) {
            KyShicaiModel *model = [[KyShicaiModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [_sectionDataArray addObject:model];
         
        }
        [_collectionview reloadData];
        [_progressView removeFromSuperview];
    }];
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath//每个cell的大小
{
    if (indexPath.row == 0 )
    {
        return CGSizeMake(169 * wt, 100 * ht);
    }
    else
    {
        return CGSizeMake(78 * wt, 40 * ht);
    }
}
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 13 * wt;  //这是最小列间距
}
#pragma mark 头视图的定义
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    HeaderCollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"identifier2" forIndexPath:indexPath];
    header.label.text = [_sectionDataArray[indexPath.section] text];
    header.label.font = [UIFont systemFontOfSize:[FontSize[0] integerValue]];
    return header;
}
//头视图的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(SCREENWIDTH, 40 * ht);
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    KyShicaiModel *model = _sectionDataArray[section];
    return [model.data count]  + 1;
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return _sectionDataArray.count;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
